/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.verify;

import picounit.features.terminal_reason.ReasonRegistry;
import picounit.features.verify.constraint.EvaluatingReasonCapturer;
import picounit.features.verify.constraint.Stringer;
import picounit.features.verify.constraint.UnconditionalEvaluator;
import picounit.verify.ReasonCapturer;
import picounit.verify.constraint.Constraint;

public class ConditionalEvaluator implements Evaluator {
	private final Evaluator unconditionalEvaluator;
	private final ReasonRegistry reasonRegistry;

	public ConditionalEvaluator(UnconditionalEvaluator unconditionalEvaluator,
		ReasonRegistry reasonRegistry) {

		this.unconditionalEvaluator = unconditionalEvaluator;
		this.reasonRegistry = reasonRegistry;
	}
	
	public void setValue(Object value) {
		unconditionalEvaluator.setValue(value);
	}
	
	public Object getValue() {
		return unconditionalEvaluator.getValue();
	}

	public void setReason(String reason) {
		unconditionalEvaluator.setReason(reason);
	}

	public ReasonCapturer evaluate(Constraint constraint) {
		if (reasonRegistry.reasonOccursNext()) {
			return new EvaluatingReasonCapturer(unconditionalEvaluator, constraint);
		}
		else {
			unconditionalEvaluator.evaluate(constraint);

			return NullReasonCapturer.INSTANCE;
		}
	}

	public ReasonCapturer evaluate(Constraint constraint, Stringer stringer) {
		if (reasonRegistry.reasonOccursNext()) {
			return new EvaluatingReasonCapturer(unconditionalEvaluator, constraint, stringer);
		}
		else {
			unconditionalEvaluator.evaluate(constraint, stringer);
	
			return NullReasonCapturer.INSTANCE;
		}
	}
}
